﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_YUESHENBAOAddControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_YBBTJ";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["ID"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
            }
            Persistence1.BindControl = panel1;
            BindData();
            IsLookPerson();
        }


        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btn_Insert = (Button)FindControl("btn_Insert");
                        btn_Insert.Visible = false;
                    }
                }
            }
        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            Label Label1 = (Label)FindControl("Label1");
            TextBox txtID = (TextBox)FindControl("txtID");
            TextBox txtYEAR = (TextBox)FindControl("txtYEAR");
            TextBox txtMONTH = (TextBox)FindControl("txtMONTH");
            TextBox txtQS = (TextBox)FindControl("txtQS");
            Label lblQS = (Label)FindControl("lblQS");
            string strqs = HttpContext.Current.Request["qs"];
            string stryear = HttpContext.Current.Request["year"];
            string strmonth = HttpContext.Current.Request["month"];
            string strid = HttpContext.Current.Request["id"];
            if (!string.IsNullOrEmpty(strqs)) { txtQS.Text = strqs; }
            if (!string.IsNullOrEmpty(stryear)) { txtYEAR.Text = stryear; }
            if (!string.IsNullOrEmpty(strmonth)) { txtMONTH.Text = strmonth; }
            if (string.IsNullOrEmpty(strid)) { txtID.Text = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper(); } else { txtID.Text = strid; }
            if (!string.IsNullOrEmpty(base.Page.Request["year"]) && !string.IsNullOrEmpty(base.Page.Request["month"]))
            {
                Label1.Text = base.Page.Request["year"] + "年" + base.Page.Request["month"];
            }
            lblQS.Text = _EluZSDataProvider.GetParamsOne("区属", strqs);

            TextBox 征收决定号 = (TextBox)FindControl("txt征收决定号");
            TextBox 征收部门 = (TextBox)FindControl("txt征收部门");
            TextBox 征收实施单位 = (TextBox)FindControl("txt征收实施单位");
            TextBox 项目地点 = (TextBox)FindControl("txt项目地点");
            TextBox 项目名称 = (TextBox)FindControl("txt项目名称");
            if (!string.IsNullOrEmpty(strid)) 
            {
                string sql = @"Select t1.征收许可证号,t.项目名称,t.项目地址,t.征收部门,t.征收实施单位 
From zs_prono t1,zs_project t Where t1.pid=t.id And t1.id In (Select pnid From zs_ybbtj Where Id='" + strid + "')";
                DataTable dt = _EluZSDataProvider.GetDTTable(sql);
                if (dt.Rows.Count > 0) {
                    征收决定号.Text = dt.Rows[0]["征收许可证号"].ToString();
                    征收部门.Text = dt.Rows[0]["征收部门"].ToString();
                    征收实施单位.Text = dt.Rows[0]["征收实施单位"].ToString();
                    项目地点.Text = dt.Rows[0]["项目地址"].ToString();
                    项目名称.Text = dt.Rows[0]["项目名称"].ToString();
                }
            }

        }
        protected void BindData() {
            Button btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
        }
        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            MPCalendar txt征收期限1 = (MPCalendar)FindControl("txt征收期限1");
            MPCalendar txt征收期限2 = (MPCalendar)FindControl("txt征收期限2");
            if (string.IsNullOrEmpty(txt征收期限1.Text) && string.IsNullOrEmpty(txt征收期限2.Text)) {
                Antu.Utilities.UtilitiesPage.AlertDialog("征收期限必须填写！", "history.back();");
                return;
            }
            if (Convert.ToDateTime(txt征收期限1.Text) > Convert.ToDateTime(txt征收期限2.Text)) {
                Antu.Utilities.UtilitiesPage.AlertDialog("征收期限开始时间必须小于等于结束时间！", "history.back();");
                return;
            }
            TextBox txtID = (TextBox)FindControl("txtID");
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            _EluZSDataProvider.UpdateBSZT("ZS_YBBTJ", "can_sync", "1", "id", txtID.Text);
            HttpContext.Current.Response.Redirect("ZS_YUESHENBAOInfo.aspx?qs=" + base.Page.Request["qs"] + "&year=" + base.Page.Request["year"] + "&month=" + base.Page.Request["month"]);
        }
    }
}


